#ifndef _HTTPSERVER_H__
#define _HTTPSERVER_H__

#include <functional>
#include "Utils/tcpserver.h"
#include "httprequest.h"
#include "httpresponse.h"

namespace tiny_muduo
{
    static const int kThreadNums = 8;
    
    class EventLoop;
    class TcpConnection;
    class Address;

    class HttpServer
    {
        typedef std::function<void(const HttpRequest&, HttpResponse&)> HttpResponseCallback;
    public:
        HttpServer(EventLoop* loop, const Address& address);
        ~HttpServer();

        void Start()
        {
            server_.Start();
        }

        void HttpDefaultCallback(const HttpRequest& request, HttpResponse& response)
        {
            response.SetStatusCode(k404NotFound);
            response.SetStatusMessage("Not Found");
            response.SetCloseConnection(true);
        }

        void ConnectionCallback(const TcpConnectionPtr& connection, Buffer* buffer)
        {
            // printf("%s: HttpServer 有新的连接！\n", __FUNCTION__);
        }

        void MessageCallback(const TcpConnectionPtr& connection, Buffer* buffer);
        
        void SetHttpResponseCallback(const HttpResponseCallback& response_callback)
        {
            response_callback_ = std::move(response_callback);
        }

        void DealWithRequest(const HttpRequest& request, const TcpConnectionPtr& connection);



        void TestTimer_IntervalEvery3Seconds() const {
            printf("%s TestTimer_IntervalEvery3Seconds\n", Timestamp::Now().ToFormattedString().data());
        }

        void TestTimer_IntervalEvery10Seconds() const {
            printf("%s TestTimer_IntervalEvery10Seconds\n", Timestamp::Now().ToFormattedString().data());
        }


    private:
        EventLoop* loop_;
        TcpServer server_;

        HttpResponseCallback response_callback_;
    };
}

#endif
